<?php
/**
 * User Class
 *
 */
class User extends MyObject {

	protected $id;
	protected $phone;
	protected $alias;
	protected $longitude;
	protected $latitude;
	protected $location_date;
	protected $fb_id;
	protected $fb_session;
	protected $fb_secret;
	protected $tw_token;
	protected $tw_secret;
	
	protected $fb_permissions;
	
	// SQL
	private static $init = false;
	private static $bd_insert;
	private static $sql_insert = 'INSERT INTO pfe_user (phone, alias, longitude, latitude, location_date, fb_id, fb_session, fb_secret, tw_token, tw_secret) VALUES (?,?,?,?,?,?,?,?,?,?)';
	private static $bd_getbyphone;
	private static $sql_getbyphone = 'SELECT * FROM pfe_user WHERE phone = ? OR alias = ? LIMIT 1';
	private static $bd_getbyfbid;
	private static $sql_getbyfbid = 'SELECT * FROM pfe_user WHERE fb_id = ?';
	private static $bd_update;
	private static $sql_update = 'UPDATE pfe_user SET phone = ?, alias = ?, longitude = ?, latitude = ?, location_date = ?, fb_id = ?, fb_session = ?, fb_secret = ?, tw_token = ?, tw_secret = ? WHERE id = ?';
	
	// Facebook
	public static $fb_api = null;
	private static $fql_fb_perm = "SELECT offline_access, publish_stream FROM permissions WHERE uid=";
	public static $OFFLINE_ACCESS = 1;
	public static $PUBLISH_STREAM = 2;
	
	
	public function __construct() {
		
		parent::__construct();
		
		$this->id = 0;
		$this->phone = '';
		$this->alias = '';
		$this->longitude = 0;
		$this->latitude = 0;
		$this->location_date = 0;
		$this->fb_id = '';
		$this->fb_session = '';
		$this->fb_secret = '';
		$this->tw_token = '';
		$this->tw_secret = '';
		
		$this->fb_permissions = null;
		
	}
	
	public static function init() {
		
		if(self::$init)
			return true;
		
		$mysql = Connection::getInstance();
		
		self::$bd_insert = $mysql->prepare(self::$sql_insert);
		self::$bd_getbyphone = $mysql->prepare(self::$sql_getbyphone);
		self::$bd_getbyfbid = $mysql->prepare(self::$sql_getbyfbid);
		self::$bd_update = $mysql->prepare(self::$sql_update);
		
		self::$init = true;
		return true;
	}
	
	public static function setFbAPI($api) {
		self::$fb_api = $api;
	}
	
	public function loadFbPermissions() {
		if($this->fb_permissions == null) {
		
			$perm = self::$fb_api->api_client->fql_query(self::$fql_fb_perm.'"'.$this->fb_id.'"');
		
			$this->fb_permissions = 0;
			$this->fb_permissions += $perm[0]['offline_access'] == "1" ? self::$OFFLINE_ACCESS : 0;
			$this->fb_permissions += $perm[0]['publish_stream'] == "1" ? self::$PUBLISH_STREAM : 0;
		}
	}
	
	public function hasOfflineAccess() {
		$this->loadFbPermissions();
		
		return $this->fb_permissions & self::$OFFLINE_ACCESS > 0;
	}
	
	public function hasPublishStream() {
		$this->loadFbPermissions();
		
		return $this->fb_permissions & self::$PUBLISH_STREAM > 0;
	}
	
	public function getValidPhone() {
		return empty($this->alias) ? $this->phone : $this->alias;
	}
	
	
	private static function createUser($sql) {
		
		if($result = $sql->fetch(PDO::FETCH_ASSOC)) {
		
			$user = new User();
			$user->id = $result['id'];
			$user->phone = $result['phone'];
			$user->alias = $result['alias'];
			$user->longitude = $result['longitude'];
			$user->latitude = $result['latitude'];
			$user->location_date = $result['location_date'];
			$user->fb_id = $result['fb_id'];
			$user->fb_session = $result['fb_session'];
			$user->fb_secret = $result['fb_secret'];
			$user->tw_token = $result['tw_token'];
			$user->tw_secret = $result['tw_secret'];
		
			return $user;
		}
		else
			return false;
	}
	
	public static function getUserByPhone($phone) {
		self::init();
		
		$sql = self::$bd_getbyphone;
		$sql->bindValue(1, $phone, PDO::PARAM_STR);
		$sql->bindValue(2, $phone, PDO::PARAM_STR);
		$sql->execute();
		
		return self::createUser($sql);
	}
	
	public static function getUserFbID($id) {
	
		self::init();
		
		$sql = self::$bd_getbyfbid;
		$sql->bindValue(1, $id, PDO::PARAM_STR);
		$sql->execute();
		
		return self::createUser($sql);
	}
	
	public function save() {
		self::init();
		
		if($this->id == 0) {				// INSERT
			$sql = self::$bd_insert;
			$sql->bindValue(1, $this->phone, PDO::PARAM_STR);
			$sql->bindValue(2, $this->alias, PDO::PARAM_STR);
			$sql->bindValue(3, $this->longitude, PDO::PARAM_STR);
			$sql->bindValue(4, $this->latitude, PDO::PARAM_STR);
			$sql->bindValue(5, $this->location_date, PDO::PARAM_INT);
			$sql->bindValue(6, $this->fb_id, PDO::PARAM_STR);
			$sql->bindValue(7, $this->fb_session, PDO::PARAM_STR);
			$sql->bindValue(8, $this->fb_secret, PDO::PARAM_STR);
			$sql->bindValue(9, $this->tw_token, PDO::PARAM_STR);
			$sql->bindValue(10, $this->tw_secret, PDO::PARAM_STR);	
			
			$sql->execute();
			$this->id = Connection::getInstance()->lastInsertId();
		}
		else {							// UPDATE
			$sql = self::$bd_update;
			$sql->bindValue(1, $this->phone, PDO::PARAM_STR);
			$sql->bindValue(2, $this->alias, PDO::PARAM_STR);
			$sql->bindValue(3, $this->longitude, PDO::PARAM_STR);
			$sql->bindValue(4, $this->latitude, PDO::PARAM_STR);
			$sql->bindValue(5, $this->location_date, PDO::PARAM_INT);
			$sql->bindValue(6, $this->fb_id, PDO::PARAM_STR);
			$sql->bindValue(7, $this->fb_session, PDO::PARAM_STR);
			$sql->bindValue(8, $this->fb_secret, PDO::PARAM_STR);
			$sql->bindValue(9, $this->tw_token, PDO::PARAM_STR);
			$sql->bindValue(10, $this->tw_secret, PDO::PARAM_STR);
			
			$sql->bindValue(11, $this->id, PDO::PARAM_INT);
			
			$sql->execute();
		}
	}
	
	
	
	/*
	public function fill($id, $login, $email, $pwd, $name, $date, $lastlogin) {
		$this->id = $id;
		$this->login = $login;
		$this->email = $email;
		$this->pwd = $pwd;
		$this->name = $name;
		$this->date = $date;
		$this->lastlogin = $lastlogin;
	}
	*/
	
	
	/**
	 * Delete the user from database
	 *
	 */
	/*
	public function delete() {
		$mysql = Connexion::getInstance();
		
		if($this->id != 0 && is_numeric($this->id)) {
			$sql = $mysql->prepare('DELETE FROM '.BDD_PREFIX.'user WHERE id = ?');
			$sql->bindValue(1, $this->id, PDO::PARAM_INT);
			$sql->execute();
		}
		else throw new Exception('l\'attribut "id" doit être défini.');
	}
	*/
}
?>
